﻿using MyDatabase;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DataBaseDAO
{
    public class GoodDAO
    {
        /// <summary>
        /// 根据keyWord模糊查询数据
        /// </summary>
        /// <param name="keyWord"></param>
        /// <returns></returns>
        public List<Good> GetGoodsList(String keyWord)
        {
            // 实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();
            // 查询需要的数据
            var data = db.Goods.ToList();
            // 如果keyWrod不为空,就模糊查询
            if (!string.IsNullOrEmpty(keyWord))
            {
                data = data.Where(p => p.Goodname.Contains(keyWord)).ToList();
            }
            return data;
        }
        /// <summary>
        /// 根据商品种类模糊查询数据
        /// </summary>
        /// <param name="keyWord"></param>
        /// <returns></returns>
        public List<Good> GetGoodsListByType(String keyWord)
        {
            // 实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();
            // 查询需要的数据
            var data = db.Goods.ToList();
            // 如果keyWrod不为空,就模糊查询
            if (!string.IsNullOrEmpty(keyWord))
            {
                data = data.Where(p => p.Goodtype.Contains(keyWord)).ToList();
            }
            return data;
        }
        /// <summary>
        /// 根据id删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelGood(int id)
        {
            // 实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();
            // 根据id查找数据
            var data = db.Goods.Where(p => p.Id == id).FirstOrDefault();
            // 如果data不为空，即查找到后进行删除
            if(data != null)
            {
                db.Goods.Remove(data);
                db.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 获取单条商品数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Good GetGoodById(int id)
        {
            // 实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();
            // 根据id查找数据
            var data = db.Goods.Where(p => p.Id == id).FirstOrDefault();
            return data;
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="good"></param>
        public void UpdateGood(Good good)
        {
            // 实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();
            // 根据id查找数据
            var data = db.Goods.Where(p => p.Id == good.Id).FirstOrDefault();
            // good的值赋值到旧的数据上
            data.Goodname = good.Goodname;
            data.Goodprice = good.Goodprice;
            data.Goodbrand = good.Goodbrand;
            data.Goodtype = good.Goodtype;
            // 保存更改
            db.SaveChanges();
        }
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="good"></param>
        public void InsertGood(Good good)
        {
            // 实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();
            // 插入数据
            db.Goods.Add(good);
            // 保存更改
            db.SaveChanges();
        }
    }
}
